package com.eastfair.imaster.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.config.vo.FormFieldVO;
import com.eastfair.core.base.R;
import com.eastfair.echo.core.EchoService;
import com.eastfair.enums.SystemSourceEnum;
import com.eastfair.imaster.dto.VisitorRegisterDTO;
import com.eastfair.imaster.dto.VisitorRegisterPageQuery;
import com.eastfair.imaster.dto.VisitorRegisterSaveDTO;
import com.eastfair.imaster.dto.VisitorRegisterUpdateDTO;
import com.eastfair.imaster.entity.VisitorRegister;
import com.eastfair.imaster.enumeration.VisitorRegisterStateRegisterTypeEnum;
import com.eastfair.imaster.service.VisitorRegisterService;
import com.eastfair.questionnaire.dto.AnswerSubjectResultSaveDTO;
import com.eastfair.questionnaire.vo.DynamicQuestionnaireVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 观众注册表（预登记表）
 * </p>
 *
 * @author zjn
 * @date 2021-08-18
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/visitorRegister")
@Api(value = "VisitorRegister", tags = "观众注册表（预登记表）")
public class VisitorRegisterController extends SuperController<VisitorRegisterService, Long, VisitorRegister, VisitorRegisterPageQuery, VisitorRegisterSaveDTO, VisitorRegisterUpdateDTO> {

    @Autowired
    private EchoService echoService;
    @Autowired
    private VisitorRegisterService visitorRegisterService;

    @Override
    public void handlerResult(IPage<VisitorRegister> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<VisitorRegister> visitorRegisterList = list.stream().map((map) -> {
            VisitorRegister visitorRegister = VisitorRegister.builder().build();
            //TODO 请在这里完成转换
            return visitorRegister;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(visitorRegisterList));
    }

    /**
     * 获取观众预登记表单
     *
     * @param visitorRegisterDTO 观众参数包（语言为用户语言）
     * @return 预登记表单
     */
    @PostMapping("/getFormField")
    public R<List<FormFieldVO>> getFormField(@RequestBody @Validated({VisitorRegisterDTO.VerifyDefaultFormField.class}) VisitorRegisterDTO visitorRegisterDTO) {
        return visitorRegisterService.getFormField(visitorRegisterDTO);
    }

    /**
     * 根据用户语言获取表单
     *
     * @param visitorRegisterDTO 包含用户语言参数
     * @return 预登记表单
     */
    @PostMapping("/getFormFieldByUserLanguage")
    public R<List<FormFieldVO>> getFormFieldByUserLanguage(@RequestBody @Validated({VisitorRegisterDTO.VerifyUserFormField.class}) VisitorRegisterDTO visitorRegisterDTO) {
        return visitorRegisterService.getFormFieldByUserLanguage(visitorRegisterDTO);
    }

    /**
     * 完善信息
     *
     * @param visitorRegisterDTO 观众信息
     * @return
     */
    @PostMapping("/addVisitor")
    public R<Long> addVisitor(@RequestBody VisitorRegisterDTO visitorRegisterDTO) {
        return visitorRegisterService.addVisitor(visitorRegisterDTO, SystemSourceEnum.IMASTER, VisitorRegisterStateRegisterTypeEnum.IMASTER);
    }

    /**
     * B展通来源完善信息
     *
     * @param visitorRegisterDTO 观众信息
     * @return
     */
    @PostMapping("/receiveVisitorBzt")
    public R<Long> receiveVisitorBzt(@RequestBody VisitorRegisterDTO visitorRegisterDTO) {
        return visitorRegisterService.addVisitor(visitorRegisterDTO, SystemSourceEnum.BZT, VisitorRegisterStateRegisterTypeEnum.BZT);
    }

    /**
     * 获取观众预登记问卷
     *
     * @return 观众预登记问卷
     */
    @GetMapping("/getVisitorRegisterQuestionnaire")
    public R<DynamicQuestionnaireVO> getVisitorRegisterQuestionnaire() {
        return visitorRegisterService.getVisitorRegisterQuestionnaire();
    }

    /**
     * 回答问卷
     *
     * @param answerSubjectResultSaveDTO 回答问卷信息
     * @return 观众胸牌卡号
     */
    @PostMapping("/saveAnswerSubjectResult")
    public R<String> saveAnswerSubjectResult(@RequestBody AnswerSubjectResultSaveDTO answerSubjectResultSaveDTO) {
        return visitorRegisterService.saveAnswerSubjectResult(answerSubjectResultSaveDTO);
    }

    /**
     * 回答问卷
     *
     * @param answerSubjectResultSaveDTO 回答问卷信息
     * @return 观众胸牌卡号
     */
    @PostMapping("/baoLi/saveAnswerSubjectResult")
    public R<String> saveAnswerSubjectResultBaoLi(@RequestBody AnswerSubjectResultSaveDTO answerSubjectResultSaveDTO) {
        return visitorRegisterService.saveAnswerSubjectResultBaoLi(answerSubjectResultSaveDTO);
    }

    /**
     * 人脸识别接口
     *
     * @param visitorRegisterDTO 人脸信息
     * @return 识别结果
     */
    @PostMapping("/getFaceIdentify")
    public R<Boolean> getFaceIdentify(@RequestBody VisitorRegisterDTO visitorRegisterDTO) {
        return visitorRegisterService.getFaceIdentify(visitorRegisterDTO);
    }


    @GetMapping("/test")
    public R<String> test() {
        baseService.testTag();
        return R.successDef();
    }

}
